<?php
/**
 * 查询推广位【申请】 jd.union.open.position.query
 *
 * See: https://union.jd.com/openplatform/api/656
 * User: Ken.Zhang <kenphp@yeah.net>
 * Date: 2019-05-12
 */
class UnionOpenPositionQueryRequest {

    /**
     * 需要创建的目标联盟id
     */
    private $unionId;

    /**
     * 目标联盟ID对应的授权key，在联盟推广管理页领取
     */
    private $key;

    /**
     * 1：cps推广位 2：cpc推广位
     */
    private $unionType;

    /**
     * 页码，上限10000
     */
    private $pageIndex;

    /**
     * 每页条数，上限100
     */
    private $pageSize;

    /**
     * 请求参数
     */
    public $apiParams = [];

    public function setUnionId($union_id)
    {
        $this->unionId = $union_id;
        $this->apiParams['positionReq']['unionId'] = $union_id;
    }

    public function getUnionId()
    {
        return $this->unionId;
    }

    public function setKey($key)
    {
        $this->key = $key;
        $this->apiParams['positionReq']['key'] = $key;
    }

    public function getKey()
    {
        return $this->key;
    }

    public function setUnionType($union_type)
    {
        $this->unionType = $union_type;
        $this->apiParams['positionReq']['unionType'] = $union_type;
    }

    public function getUnionType()
    {
        return $this->unionType;
    }

    public function setPageIndex($page_index)
    {
        $this->pageIndex = $page_index;
        $this->apiParams['positionReq']['pageIndex'] = $page_index;
    }

    public function getPageIndex()
    {
        return $this->pageIndex;
    }

    public function setPageSize($page_size)
    {
        $this->pageSize = $page_size;
        $this->apiParams['positionReq']['pageSize'] = $page_size;
    }

    public function getPageSize()
    {
        return $this->pageSize;
    }

    public function getApiMethodName()
    {
        return 'jd.union.open.position.query';
    }

    /**
     * 解析结果
     *
     * @param   string      $response
     * @throws  Exception
     * @return  mixed
     */
    public function getResult($response)
    {
        $responseKey = str_replace('.', '_', $this->getApiMethodName()) . '_response';

        $response = @json_decode($response, true);
        if (!array_key_exists($responseKey, $response))
            $resp = $response['errorResponse'];
        else {
            $response[$responseKey]['result'] = json_decode($response[$responseKey]['result']);

            $resp = $response[$responseKey];
        }

        return $resp;
    }

}